"""Add alarm tables.

Revision ID: 20251018_1549
Revises:
Create Date: 2025-10-18 15:49:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20251018_1549'
down_revision = None
depends_on = None


def upgrade() -> None:
    """Create alarm_rules and alarm_history tables."""
    # Create alarm_rules table
    op.create_table(
        'alarm_rules',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('device_id', sa.Integer(), nullable=False),
        sa.Column('metric_name', sa.String(255), nullable=False),
        sa.Column('description', sa.String(500), nullable=True),
        sa.Column('alarm_type', sa.Enum('threshold', 'change_rate', 'duration', name='alarmtype'), nullable=False),
        sa.Column('severity', sa.Enum('info', 'warning', 'error', 'critical', name='alarmseverity'), nullable=False),
        sa.Column('threshold_high', sa.Float(), nullable=True),
        sa.Column('threshold_low', sa.Float(), nullable=True),
        sa.Column('change_rate_threshold', sa.Float(), nullable=True),
        sa.Column('duration_threshold', sa.Integer(), nullable=True),
        sa.Column('enabled', sa.Boolean(), nullable=False, server_default='1'),
        sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
        sa.Column('updated_at', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
        sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for alarm_rules
    op.create_index('idx_alarm_rules_device_metric', 'alarm_rules', ['device_id', 'metric_name', 'enabled'])
    op.create_index(op.f('ix_alarm_rules_device_id'), 'alarm_rules', ['device_id'])
    op.create_index(op.f('ix_alarm_rules_id'), 'alarm_rules', ['id'])

    # Create alarm_history table
    op.create_table(
        'alarm_history',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('rule_id', sa.Integer(), nullable=False),
        sa.Column('device_id', sa.Integer(), nullable=False),
        sa.Column('metric_name', sa.String(255), nullable=False),
        sa.Column('trigger_value', sa.Float(), nullable=False),
        sa.Column('trigger_time', sa.DateTime(), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
        sa.Column('severity', sa.Enum('info', 'warning', 'error', 'critical', name='alarmseverity'), nullable=False),
        sa.Column('message', sa.String(500), nullable=True),
        sa.Column('acknowledged', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('acknowledged_at', sa.DateTime(), nullable=True),
        sa.Column('acknowledged_by', sa.Integer(), nullable=True),
        sa.Column('acknowledged_note', sa.String(500), nullable=True),
        sa.ForeignKeyConstraint(['rule_id'], ['alarm_rules.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['acknowledged_by'], ['users.id']),
        sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for alarm_history
    op.create_index('idx_alarm_history_device_time', 'alarm_history', ['device_id', 'trigger_time'])
    op.create_index('idx_alarm_history_rule_time', 'alarm_history', ['rule_id', 'trigger_time'])
    op.create_index('idx_alarm_history_acknowledged', 'alarm_history', ['acknowledged', 'trigger_time'])
    op.create_index(op.f('ix_alarm_history_device_id'), 'alarm_history', ['device_id'])
    op.create_index(op.f('ix_alarm_history_id'), 'alarm_history', ['id'])
    op.create_index(op.f('ix_alarm_history_trigger_time'), 'alarm_history', ['trigger_time'])


def downgrade() -> None:
    """Drop alarm tables."""
    op.drop_index(op.f('ix_alarm_history_trigger_time'), table_name='alarm_history')
    op.drop_index(op.f('ix_alarm_history_id'), table_name='alarm_history')
    op.drop_index(op.f('ix_alarm_history_device_id'), table_name='alarm_history')
    op.drop_index('idx_alarm_history_acknowledged', table_name='alarm_history')
    op.drop_index('idx_alarm_history_rule_time', table_name='alarm_history')
    op.drop_index('idx_alarm_history_device_time', table_name='alarm_history')
    op.drop_table('alarm_history')

    op.drop_index(op.f('ix_alarm_rules_id'), table_name='alarm_rules')
    op.drop_index(op.f('ix_alarm_rules_device_id'), table_name='alarm_rules')
    op.drop_index('idx_alarm_rules_device_metric', table_name='alarm_rules')
    op.drop_table('alarm_rules')

    # Drop enum types
    op.execute('DROP TYPE IF EXISTS alarmseverity')
    op.execute('DROP TYPE IF EXISTS alarmtype')
